
/**
 * 数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const listToTree = (list, tree, parentId, transfObj) => {
  list.forEach(item => {
    // 判断是否为父级菜单
    if (item.parentId === parentId) {
      const child = {
        title: item[transfObj.title],
        key: item[transfObj.key],
        scopedSlots: { title: 'title' },
        children: []
      };
      // 迭代 list， 找到当前菜单相符合的所有子菜单
      listToTree(list, child.children, item.id, transfObj);
      // 删掉不存在 children 值的属性
      if (child.children.length <= 0) {
        delete child.children;
      }
      // 加入到树中
      tree.push(child);
    }
  });
};


export const listToSelectTree = (list, tree, transfObj) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.treeLeaf){
      item.children = [];
    }
    treeMap.set(item.id,item)
  });

  list.forEach(item => {
    item.key = item[transfObj.key];
    item.value = item[transfObj.key];
    item.title = item[transfObj.title];
    let parentId = item.parentId;
    let parentItem = treeMap.get(parentId);
    if(parentItem ){
      let {children} = parentItem;
      if(children){
        children.push(item);
      }else{
        parentItem.children = [item];
      }
      item.hasParent = true;
    }
  });
  
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("listToSelectTree_treeMap",treeMap,tree);
};

/**
 * 数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const pageListToTree = (list, tree ) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.treeLeaf){
      item.children = [];
    }
    treeMap.set(item.id,item)
  });

  list.forEach(item => {
      //是否开启搜索
      item.key = item.id;
      let parentId = item.parentId;
      let parentItem = treeMap.get(parentId);
      if(parentItem ){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
    // // 判断是否为父级菜单
    // if (item.parentId === parentId) {
    //   const child = {
    //     ...item,
    //     key: item.id,
    //     children: []
    //   };
    //   // 迭代 list， 找到当前菜单相符合的所有子级
    //   pageListToTree(list, child.children, item.id);
    //   // 删掉不存在 children 值的属性
    //   if (child.children.length <= 0) {
    //     delete child.children;
    //   }
    //   // 加入到树中
    //   tree.push(child);
    // }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("tree..",tree)
};



  /**
   * 搜索树方法
   * @param {树集合} tree 
   * @param {节点字段名字} key 
   * @param {搜索关键字} seachKey 
   */
  export const searchInTreeData = (expandedKeys,tree, key ,seachKey)=>{
    let findNodes = new Array();
    tree && tree.forEach && tree.forEach(item=>{
      let node = {...item};
      let findChildrenNodes;
      //1.优先判断 是否有子节点
      if(node.children){
        findChildrenNodes = searchInTreeData(expandedKeys,node.children, key ,seachKey);
        if(findChildrenNodes && findChildrenNodes.length > 0){
          node.children = findChildrenNodes;
          findNodes.push(node);
          expandedKeys.push(node.key);
          return true;
        } 
      }
      //判断本节点
      if(isHaveSearchKey(node,key,seachKey)){
        delete node.children;
        findNodes.push(node);
      }
    });
    return findNodes;
  }


  // //查找节点
  // const findNode = (tree , key ,seachKey) =>{
   

  // }
  //是否包含 搜索值
  const isHaveSearchKey = (node , key ,seachKey) =>{
    if(!node || !key || !seachKey)return false;
    const value = node[key];
    if(!value)return false;
    return value.indexOf(seachKey) > -1;
  }


/**
 * 地区数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const areaPageListToTree = (list, tree) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.treeLeaf){
      item.children = [];
    }
    treeMap.set(item.areaCode,item)
  });

  list.forEach(item => {
      item.key = item.areaCode;
      item.title =  item.areaName;
      let parentCode = item.parentCode;
      let parentItem = treeMap.get(parentCode);
      if(parentItem){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("tree..",tree);
};

export const areaPageListToTreeForId = (list, tree) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.treeLeaf){
      item.children = [];
    }
    treeMap.set(item.areaCode,item)
  });

  list.forEach(item => {
      item.key = item.id;
      item.title =  item.areaName;
      let parentCode = item.parentCode;
      let parentItem = treeMap.get(parentCode);
      if(parentItem){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("tree..",tree);
};

/**
 * 机构/用户转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const officeUserListToTree = (list, tree) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.treeLeaf){
      item.children = [];
    }
    treeMap.set(item.id, item);
  });

  list.forEach(item => {
      item.key = item.id;
      item.title =  item.name;
      if(item.key.indexOf("user") != -1) {
        item.slots = {icon: 'user'};
      }else{
        // item.disableCheckbox = true;
      }
      let parentCode = item.parentId;
      let parentItem = treeMap.get(parentCode);
      if(parentItem){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
};


/**
 * 会员数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const userPageListToTree = (list, tree) => {
  const treeMap = new Map();
  list.forEach(item=>{
    // if(!item.treeLeaf){
    // }
    item.children = [];
    treeMap.set(item.id, item)
  });

  list.forEach(item => {
      item.key = item.id;
      item.title =  item.realName;
      let parentCode = item.shareUserId;
      let parentItem = treeMap.get(parentCode);
      if(parentItem){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("tree..",tree);
};


/**
 * 地区数组转树形结构
 * @param list 源数组
 * @param tree 树
 * @param parentId 父ID
 */
export const classPageListToTree = (list, tree) => {
  const treeMap = new Map();
  list.forEach(item=>{
    if(!item.classEnd){
      item.children = [];
    }
    treeMap.set(item.id, item)
  });

  list.forEach(item => {
      item.key = item.id;
      item.title =  item.className;
      let parentId = item.parentId;
      let parentItem = treeMap.get(parentId);
      if(parentItem){
        let {children} = parentItem;
        if(children){
          children.push(item);
        }else{
          parentItem.children = [item];
        }
        item.hasParent = true;
      }
  });
  tree.push( ...list.filter(item=>!item.hasParent));
  console.log("tree..",tree);
};